package com.zyp.servlet;

import com.zyp.service.StudentService;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "pagingQueryServlet", urlPatterns = "/pagingQueryServlet")
public class pagingQueryServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        //获取到从前端传过来的更新的值
        int store = 0;
        Cookie[] cookies = request.getCookies();
        //获取到的cook中的储存的值
        for (Cookie cookie : cookies) {
            //当循环到的值为yeshu时，进行存储，然后更新Cookie进行修改
            if (cookie.getName().equals("yeshu")) {
                //存储值
                String a;
                Cookie c;
                //获取到当前的Cookie
                store = Integer.parseInt(cookie.getValue());
                //判断是进行向上取值，还是向下取值
                String previousPage = request.getParameter("previousPage");
                if (previousPage != null && store != 0) {
                    //数值的转化
                    a = String.valueOf(store - 5);
                    c = new Cookie("yeshu", a);
                    //更新Cookie的值
                    response.addCookie(c);

                }
                String pextPage = request.getParameter("pextPage");
                if (pextPage != null) {
                    //数值的转化
                    a = String.valueOf(store + 5);
                    c = new Cookie("yeshu", a);
                    //更新Cookie的值
                    response.addCookie(c);
                }


                System.out.println(store);
                //调用SQL，获取到值
                StudentService ss = new StudentService();
                List<Object[]> list = ss.getPagingQueryService(store);
                //
                for (Object[] objects : list) {
                    System.out.println(objects);
                }
                //将值存储到Session
                request.getSession().setAttribute("NumericalValue", list);

            }
        }

        response.sendRedirect("http://localhost:8081/task_4module_1/pagingQuery.jsp");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
